﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using FinPlusCompCore;
using QLNet;
using p = FinPlusCompQuant.QLConvParser;


namespace FinPlusCompQuant
{
    public class FixedRateBondBuild : FinPlusComponent
    {
        public RelinkableHandle<Quote> CleanPrice { get; private set; }

        //construct
        public FixedRateBondBuild(string marketName, string name, double coupon, double cleanPrice, string couponFrq,  double redemption, 
            int settlementDays, DateTime effectiveDate, DateTime terminationDate, string dayCount, string payConv, string termConv, 
            string bondConv, string holidays, bool endOfMonth = false)
        {
            var market = Markets.Instance.GetMarket(marketName);

            CleanPrice = market.GetQuote(name);
            CleanPrice.linkTo(new SimpleQuote(cleanPrice));

            var calendar = p.Calendar(holidays);

            var schedule = new Schedule(effectiveDate, terminationDate, new Period(p.Freq(couponFrq)), calendar, 
                p.BizConv(payConv), p.BizConv(termConv), DateGeneration.Rule.Backward, endOfMonth);

		    var bond = new QLNet.FixedRateBond(settlementDays, 100, schedule, new List<double>() { coupon }, p.DayCount(dayCount), 
                p.BizConv(payConv), redemption, effectiveDate);
	
		    //used for bond curves
            var rateHelper = new FixedRateBondHelper(CleanPrice, settlementDays, 100.0, schedule, new List<double>() { coupon },
                p.DayCount(dayCount), p.BizConv(bondConv), redemption, effectiveDate);

            market.SetRateHelper(name, rateHelper);
        }
    }
}
